<?php
class HtmlUtilEx {

    function getSelector_ObjectArray ($modname, $objectType, $name, $field='id', $displayField='name',
                                      $where='', $sort='', $selectedValue='', $defaultValue=0, $defaultText='', $allValue=0, $allText='',
                                      $displayField2=null, $submit=true, $disabled=false, $fieldSeparator=', ', $multipleSize=1,
                                      $onchange='')
    {
        if (!$modname) {
            return pn_exit ('Invalid modname passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!$objectType) {
            return pn_exit ('Invalid object name passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!pnModDBInfoLoad($modname)) {
            return "Unavailable/Invalid modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }

        $id = strtr ($name, '[]', '__');
        $disabled     = $disabled ? 'disabled="disabled"' : '';
        $multiple     = $multipleSize > 1 ? 'multiple="multiple"' : '';
        $multipleSize = $multipleSize > 1 ? "size=\"$multipleSize\"" : '';
        $submit       = $submit ? 'onchange="this.form.submit();"' : '';
        $onchange     = empty($onchange) ? '' : "onchange='" . $onchange . "'";

        $html = "<select name=\"$name\" id=\"$id\" $multipleSize $multiple $submit $onchange $disabled>";

        if ($defaultText && !$selectedValue) {
            $sel = ((string)$defaultValue==(string)$selectedValue ? 'selected="selected"' : '');
            $html .= "<option value=\"$defaultValue\" $sel>$defaultText</option>";
        }

        if ($allText) {
            $sel = ((string)$allValue==(string)$selectedValue ? 'selected="selected"' : '');
            $html .= "<option value=\"$allValue\" $sel>$allText</option>";
        }

        if (!SecurityUtil::checkPermission("$objectType::", '::', ACCESS_OVERVIEW)) {
            return "Security Check failed for modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }

        $classname = Loader::loadClassFromModule ($modname, $objectType, true);
        if (!$classname) {
            return "Unable to load class [$objectType] for module [$modname]";
        }

        $class = new $classname ();
        $dataarray = $class->get ($where, $sort, -1, -1, '', false/*, $distinct*/);

        foreach ($dataarray as $object) {
            $val   = $object[$field];
            $sel   = ((string)$val==(string)$selectedValue ? 'selected="selected"' : '');
            $disp  = $object[$displayField];

            $disp2 = '';
            if ($displayField2) {
                $disp2 = $fieldSeparator . $object[$displayField2];
            }

            $html .= "<option value=\"$val\" $sel>$disp $disp2</option>";
        }

        $html .= '</select>';
        return $html;
    }

    function getChecker_ObjectArray ($modname, $objectType, $name, $field='id', $displayField='name',
                                      $where='', $sort='', $selectedValue='', $defaultValue=0, $defaultText='', $allValue=0, $allText='',
                                      $displayField2=null, $submit=true, $disabled=false, $fieldSeparator=', ', $multipleSize=1,
                                      $onchange='')
    {
        if (!$modname) {
            return pn_exit ('Invalid modname passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!$objectType) {
            return pn_exit ('Invalid object name passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!pnModDBInfoLoad($modname)) {
            return "Unavailable/Invalid modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }

        $id = strtr ($name, '[]', '__');
        $disabled     = $disabled ? 'disabled="disabled"' : '';
        $multiple     = $multipleSize > 1 ? 'multiple="multiple"' : '';
        $multipleSize = $multipleSize > 1 ? "size=\"$multipleSize\"" : '';
        $submit       = $submit ? 'onchange="this.form.submit();"' : '';
        $onchange     = empty($onchange) ? '' : "onchange='" . $onchange . "'";

        if ($defaultText && !$selectedValue) {
            $sel = ((string)$defaultValue==(string)$selectedValue ? "checked=\"yes\"" : '');
            $html = "<input type=\"checkbox\" name=\"".$name."[]\" id=\"$id\" value=\"$defaultValue\" $sel/> $defaultText";
        }

        if ($allText) {
            $sel = ((string)$allValue==(string)$selectedValue ? "checked=\"yes\"" : '');
            $html = "<input type=\"checkbox\" name=\"".$name."[]\" id=\"$id\" value=\"$allValue\" $sel/> $allText";
        }

        if (!SecurityUtil::checkPermission("$objectType::", '::', ACCESS_OVERVIEW)) {
            return "Security Check failed for modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }

        $classname = Loader::loadClassFromModule ($modname, $objectType, true);
        if (!$classname) {
            return "Unable to load class [$objectType] for module [$modname]";
        }

        $class = new $classname ();
        $dataarray = $class->get ($where, $sort, -1, -1, '', false/*, $distinct*/);

        foreach ($dataarray as $object) {
            $val   = $object[$field];
            if(is_array($selectedValue)){
              foreach($selectedValue AS $selected){
                $sel   = ((string)$val==(string)$selected ? "checked=\"yes\"" : '');
              }
            }else{
              $sel   = ((string)$val==(string)$selected ? "checked=\"yes\"" : '');
            }
            
            $disp  = $object[$displayField];

            $disp2 = '';
            if ($displayField2) {
                $disp2 = $fieldSeparator . $object[$displayField2];
            }

            $html .= "<input type=\"checkbox\" name=\"".$name."[]\" id=\"$id\" value=\"$val\" $sel/> $disp $disp2";
        }
        return $html;
    }
    
    
    function getSelector_ForSort ($modname, $objectType, $name, $field='id', $displayField='name',
                                      $where='', $sort='', $selectedValue='', $defaultValue=0, $defaultText='', $allValue=0, $allText='',
                                      $displayField2=null, $submit=true, $disabled=false, $fieldSeparator=', ', $multipleSize=1,
                                      $onchange='')
    {
        if (!$modname) {
            return pn_exit ('Invalid modname passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!$objectType) {
            return pn_exit ('Invalid object name passed to HtmlUtil::getSelector_ObjectArray ...');
        }

        if (!pnModDBInfoLoad($modname)) {
            return "Unavailable/Invalid modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }
        
        
        $server   = pnGetHost();
        $protocol = pnServerGetProtocol();
        $baseurl  = "$protocol://$server";
        //$request = print_r($params['query'],true);
        $get = $_GET;
        $query = explode('&',$params['query']);
        foreach($query as $q){
          $item = explode('=',$q);
          $key = $item[0];
          $value = $item[1];
          $get[$key] = $value;
        }
      
        $scriptname = pnServerGetVar('SCRIPT_NAME');
        $pathinfo = pnServerGetVar('PATH_INFO');
      
        if ($pathinfo == $scriptname) {
          $pathinfo = '';
        }
        //print_r($query);
        //print_r($get);
      
        $results = array();
        foreach($get as $k => $v){
          if (!empty($k)&&$k!="hotelid"){
            $results[$k] = $k . '=' . $v;
          }
        }
        //print_r($results);
        $result = $baseurl . $scriptname . $pathinfo .'?'. implode('&',$results);

        
        $id = strtr ($name, '[]', '__');
        $disabled     = $disabled ? 'disabled="disabled"' : '';
        $multiple     = $multipleSize > 1 ? 'multiple="multiple"' : '';
        $multipleSize = $multipleSize > 1 ? "size=\"$multipleSize\"" : '';
        $submit       = $submit ? 'onchange="this.form.submit();"' : '';
        $onchange     = empty($onchange) ? "onchange='document.location.href=this.value'" : "onchange='" . $onchange . "'";
        
        $html = "<select name=\"$name\" id=\"$id\" $multipleSize $multiple $submit $onchange $disabled>";

        if ($defaultText && !$selectedValue) {
            $sel = ((string)$defaultValue==(string)$selectedValue ? 'selected="selected"' : '');
            $html .= "<option value=\"$defaultValue\" $sel>$defaultText</option>";
        }

        if ($allText) {
            $sel = ((string)$allValue==(string)$selectedValue ? 'selected="selected"' : '');
            $html .= "<option value=\"$result\" $sel>$allText</option>";
        }

        if (!SecurityUtil::checkPermission("$objectType::", '::', ACCESS_OVERVIEW)) {
            return "Security Check failed for modulename [$modname] passed to HtmlUtil::getSelector_ObjectArray";
        }

        $classname = Loader::loadClassFromModule ($modname, $objectType, true);
        if (!$classname) {
            return "Unable to load class [$objectType] for module [$modname]";
        }

        $class = new $classname ();
        $dataarray = $class->get ($where, $sort, -1, -1, '', false/*, $distinct*/);

        foreach ($dataarray as $object) {
            $val   = $object[$field];
            $sel   = ((string)$val==(string)$selectedValue ? 'selected="selected"' : '');
            $disp  = $object[$displayField];

            $disp2 = '';
            if ($displayField2) {
                $disp2 = $fieldSeparator . $object[$displayField2];
            }

            $html .= "<option value=\"$result&hotelid=$val\" $sel>$disp $disp2</option>";
        }

        $html .= '</select>';
        return $html;
    }

}